import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class ServesService {
  constructor() {}
  public static fourItems(value: []): number[] {
    let newValue: number[] = [];
    value.forEach((element) => {
      if (element % 4 == 0) {
        newValue.push(4);
      } else {
        newValue.push(element % 4);
      }
    });
    return newValue;
  }

  /**
   * 随机把数组打乱
   * @param _chopsticks
   * @returns
   */
  public static number_of_components(_chopsticks: number[]) {
    _chopsticks.sort(randomsort);
    function randomsort(a: any, b: any) {
      return Math.random() > 0.5 ? -1 : 1;
      //用Math.random()函数生成0~1之间的随机数与0.5比较，返回-1或1
    }
    console.log(_chopsticks);
    return _chopsticks;
  }

  public static fentwohalf(chopsticks: number[]) {
    let _random = (chopsticks.length - 4 - 1 - 4) * Math.random() + (4 + 1);
    let leftChopsticks = chopsticks.slice(0, _random);
    let rightChopsticks = chopsticks.slice(_random);
    let newList = { a: leftChopsticks, b: rightChopsticks };
    return newList;
  }

  public static checkoutResult(value: number[]) :number[]{
    let result_list:number[] = []
    value.forEach((element) => {
      if(checkoutNumber(element) == 9) {
        console.error("error");
      } else {
        result_list.push(checkoutNumber(element))
      }
      
    });

    return result_list;

    function checkoutNumber(value: number) {
      switch (value) {
        case (value = 9):
          return 1;
        case (value = 8):
          return 1;
        case (value = 6):
          return 0;
        case (value = 7):
          return 0;
        case (value = 8):
          return 1;
        default:
          return 9
      }
    }
  }
}
